<!-- 资源位编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改游戏资源位' : '添加游戏资源位'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="资源位名称:" prop="title">
        <el-input
          :maxlength="4"
          v-model="form.title"
          placeholder="请输入游戏资源位名称"
          clearable
        />
      </el-form-item>

      <el-form-item label="资源位icon:" prop="icon">
        <uploadImage :limit="1" v-model="form.icon"> </uploadImage>
      </el-form-item>
      <el-form-item label="资源位权重:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="权重越大排序越靠前"
          controls-position="right"
          class="ele-fluid ele-text-left"
        />
      </el-form-item>
      <el-form-item label="跳转类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">外部链接</el-radio>
          <el-radio :label="3">内部链接</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="链接:" prop="link">
        <el-input
          :maxlength="255"
          v-model="form.link"
          placeholder="请输入链接"
          clearable
        />
      </el-form-item>

      <el-form-item label="是否开启" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          v-model="form.notes"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import UploadImage from "@/components/uploadImage.vue";

export default {
  name: "BoxGameResourcePositionEdit",
  components: { UploadImage },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  // components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 }, this.data),
      // 表单验证规则
      rules: {
        title: [{ required: true, message: "请输入名称", trigger: "blur" }],
        icon: [{ required: true, message: "请输入icon", trigger: "blur" }],
        link: [{ required: true, message: "请输入链接", trigger: "blur" }],
        type: [{ required: true, message: "请选择跳转类型", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "请输入状态，1、启用，0、禁用",
            trigger: "blur"
          }
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        notes: [{ required: true, message: "请输入备注", trigger: "blur" }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      bannerOptions: []
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/boxGameResourcePosition/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          if (!this.isUpdate) {
            this.form = {};
          }
        })
        .catch(() => {});
    },
  }
};
</script>

<style scoped></style>
